<template>
  <div>
    <div class="header">
      <van-sticky>
        <van-nav-bar
          title="编辑地址"
          left-text="返回"
          left-arrow
          @click-left="onClickLeft"
        />
      </van-sticky>

      <van-address-edit
        :area-list="areaList"
        show-delete
        :address-info="addressData"
        show-set-default
        show-search-result
        :search-result="searchResult"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
      />
    </div>
  </div>
</template>

<script>
import { AddressEdit } from "vant";
import { get, post_json, post } from "@/http/axios";
import { areaList } from "@vant/area-data";
import { Toast } from "vant";
export default {
  data() {
    return {
      areaList,
      searchResult: [],
      userid: "",

      username: "",
      telephone: "",

      addressData: [],

      id: "",
    };
  },
  methods: {
    onClickLeft() {
      this.$router.push({
        path: "/manager/mineaddress",
        query: {
          id: this.userid,
        },
      });
    },

    async onSave(content) {
      //   向对象中添加新的属性;
      content.userId = this.userid;
      let data = {
        id: content.id,
        username: content.name,
        telephone: content.tel,
        province: content.province,
        city: content.city,
        address: content.addressDetail,
        area: content.county,
        isDefault: content.isDefault == true ? 1 : 0,
        userId: content.userId,
      };
      let res = await post("/address/saveOrUpdate", data);
      Toast.success("添加成功！");
      this.$router.push({
        path: "/manager/mineaddress",
        query: {
          id: this.userid,
        },
      });
    },
    //删除功能
    async onDelete(content) {
      let obj = {
        id: content.id,
      };
      let res = await get("/address/deleteById", obj);
      Toast.success("删除成功！");
      this.$router.push({
        path: "/manager/mineaddress",
        query: {
          id: this.userid,
        },
      });
    },
  },
  created() {
    //传递userid
    this.userid = this.$route.query.userid;
    this.addressData = this.$route.query.item;
    this.id = this.$route.query.item.id;
  },
};
</script>

<style scoped>
</style>